import Utils from "../utils"
import GameObject from "../GameObject"
import FieldObject from "../FieldObject"
import GameConst from "../GameConst";
import  SoundsManager from "../soundmanager"
 
export default class GemDestroyAnimation extends GameObject{
    
    constructor(n) {
        super();
        // this.totalFrames = 0;
        this.playedScore = false;
        this.color = n;
        // this.totalFrames = GemDestroyAnimation.animLen[n];
        this.fileName = FieldObject.assetNames[this.color] + "x";
        this.sprite = Utils.createAnimation(this.fileName);
        this.sprite.framerate = 30;
        var r = GemDestroyAnimation.regPoints[this.color];
        // this.sprite.scaleX = this.sprite.scaleY = 1.5;
        // this.sprite.pivotX = r.x / this.sprite.scaleX;
        // this.sprite.pivotY = r.y / this.sprite.scaleY;
        this.sprite.scaleX = this.sprite.scaleY = this.sprite.scaleX * GameConst.TILE_SIZE_FACTOR;
    }
    
    destroy() {
        if (!this.isDestroyed) {

            this.cell.tryPlayScoreAnimation();
            this.playedScore = true;

            GameConst.pool.returnGemDestroy(this);
        }
    };
    // update(e) {
    //     if (this.isDestroyed)
    //         return;
    //     var t = this.sprite.currentAnimationFrame;
    //     if (!this.playedScore && t > this.totalFrames / 2) {
    //         this.cell.tryPlayScoreAnimation();
    //         this.playedScore = true;
    //     }
    //     if (t > this.totalFrames) {
    //         GameConst.pool.returnGemDestroy(this);
    //     }
    // };

    onLabel(e){
        console.log(e);
        if(e.name=='middle'){
            this.cell.tryPlayScoreAnimation();
            this.playedScore = true;
        }
    }
    init(x, y, cell) {
        this.cell = cell;

        Utils.addChild(this.sprite, GameConst.level.gemDestroyLayer);
        // this.sprite.gotoAndPlay(this.fileName);
        this.sprite.once(Laya.Event.COMPLETE,this,this.destroy);
        this.sprite.on(Laya.Event.LABEL,this,this.onLabel);
        this.sprite.play(0,false);
        this.sprite.x = x;
        this.sprite.y = y;
        GameConst.level.objects.push(this);
        this.isDestroyed = false;
        this.playedScore = false;
        if (this.color < GameConst.level.assetNumber) {
            SoundsManager.instance.playSound("remove" + (Utils.getInt(4) + 1));
        }
    };
    release() {
        this.sprite.stop();
        this.sprite.offAll();
        Utils.removeClip(this.sprite);
        this.isDestroyed = true;
    };
    // t.animLen = [16, 13, 16, 17];
    // t.animSizes = [cjp(98, 135), cjp(133, 96), cjp(110, 114), cjp(83, 118)];
    // t.regPoints = [cjp(54, 74), cjp(66, 62), cjp(52, 60), cjp(42, 38)];
    // return t;
}
// GemDestroyAnimation.animLen = [16, 13, 16, 17];
GemDestroyAnimation.animSizes = [Utils.cjp(98, 135), Utils.cjp(133, 96),Utils.cjp(110, 114), Utils.cjp(83, 118)];
GemDestroyAnimation.regPoints = [Utils.cjp(54, 74), Utils.cjp(66, 62), Utils.cjp(52, 60), Utils.cjp(42, 38)];